import Vue from 'vue'
import App from './App'
import store from './store' // 数据仓库

import router from './router/index.js'

import bgy from 'bgy'
Vue.use(bgy.install)

import bgymain from 'bgymain'
Vue.use(bgymain.install)

import weather from './components/weather/weather.vue'
Vue.component(weather.name, weather)

Vue.prototype.$store = store

import Frame from './components/frame/frame.vue'
Vue.component(Frame.name, Frame)

import video from './components/video/video.vue'
Vue.component(video.name, video)

import mulvideo from './components/video/multiple_video.vue'
Vue.component(mulvideo.name, mulvideo)

import mulBackvideo from './components/video/multiple_backvideo.vue'
Vue.component(mulBackvideo.name, mulBackvideo)

// 全局路由拦截
import interceptionRoute from './interceptionRoute'
router.beforeEach((to, from, next) => {
  interceptionRoute(to, from, next)
})

Vue.prototype.$axios.interceptors.request.use((config) => {
  // 防止重复请求
  if (Vue.prototype._requestSet.has(config.url) && !config.closeSaveRequest) {
    console.log(`重复请求的URL${config.url}`)
    return
  }
  Vue.prototype._requestSet.add(config.url)

  // 是否关闭加载动画
  if (!config.closeLoading) {
    store.commit('showLoading')
  }

  // 去除空的key
  if (config && config.data) {
    if (Object.prototype.toString.call(config.data) === '[object String]') {
      let items = config.data.split('&')
      if (items && items.length > 0 ) {
        items.forEach((item, index) => {
          if (item) {
            let data = item.split('=')
            if (data && data.length > 0 && !data[1]) {
              items.splice(index, 1)
            }
          }
        })
        config.data = items.join('&')
      }
    }
  }

  // 是否打开分页
  if (config.paginate) {
    // 带query的为老版本 queryType为1,不带query的为新版本 queryType为2
    const queryPaginate = store.getters.getQueryPaginate
    let currentPage = `&page=${queryPaginate.currentPage}`
    let pageSize = `&pageSize=${queryPaginate.pageSize}`
    // query 是否另外命名
    if (config.queryName) {
      currentPage = `&${config.queryName}.currentPage=${queryPaginate.currentPage}`
      pageSize = `&${config.queryName}.pageSize=${queryPaginate.pageSize}`
    }
    config.data = config.data + currentPage + pageSize
  }
  return config
})

Vue.prototype.$axios.interceptors.response.use(data => {
  Vue.prototype._requestSet.delete(data.config.url)
  if (Vue.prototype._requestSet.size === 0) {
    store.commit('hideLoading')
  }
  // 设置分页参数
  if (data.config.paginate) {
    store.commit({
      type: 'muPaginate',
      query: data.data.query || data.data.data,
      pagingId: data.config.paginId || data.config.pagingId
    })
  }
  store.commit('removeQueryPaginate')
  return data
}, (error) => {
  if (error.config) {
    Vue.prototype._requestSet.delete(error.config.url)
  }
  if (error.response.data.errorMsg) {
    Vue.prototype.$message.error(error.response.data.errorMsg)
  }
  store.commit('hideLoading')
  return Promise.reject(error)
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: { App }
})
